package com.xdf.wxbebarrel.controller;

import com.xdf.wxbebarrel.model.UserMill;
import com.xdf.wxbebarrel.service.UserMillService;
import com.xdf.wxbebarrel.service.UserService;
import com.xdf.wxbebarrel.utils.DynamicDBUtils;
import com.xdf.wxbebarrel.utils.JsonResult;
import com.xdf.wxbebarrel.utils.Schedule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 开发人员使用的后台接口
 */
@Api(tags = "开发人员使用，非用户接口")
@CrossOrigin
@RestController
@RequestMapping("/dev")
public class DevController {
    @Autowired
    private DynamicDBUtils dynamicDBUtils;
    @Autowired
    private UserService userService;

    @Autowired
    private UserMillService userMillService;
    @Autowired
    private Schedule schedule;

    /**
     * 开发人员审核微信用户：1. 表 user 中设置真实姓名 2. 表 user_mill 设置用户工厂关系
     * @param openId
     * @param mill
     * @return
     */
    @GetMapping("/audit/{openId}/{realName}/{mill}")
    private JsonResult audit(@PathVariable String openId,@PathVariable String realName,
                             @PathVariable String mill){
        userService.updateRealName(openId,realName,mill);
        UserMill um = new UserMill();
        um.setOpenId(openId);
        um.setMillSid(mill);
        userMillService.save(um);
        return JsonResult.ok();
    }

    @GetMapping("/pdt")
    private JsonResult pushDeliverTest() throws Exception {
        schedule.pushDeliver();
        return JsonResult.ok("推送结束");
    }

    @PostMapping("/hello")
    private JsonResult hello(@RequestBody Map<String,String> params){
        return JsonResult.ok("接受到参数是：" + params.toString());
    }

    @PostMapping("/hello1")
    private JsonResult hello1(){
        return JsonResult.ok("无参的POST请求");
    }

    @ApiOperation(value = "执行存储过程进行查询", httpMethod = "POST", notes = "传入参数是字符串数组，依次是存储过程名称、参数1、参数2...参数N")
    @PostMapping("/queryForListCallProc")
    public JsonResult queryForListCallProc(@RequestBody List<String> procAndParameters){
        return JsonResult.ok(dynamicDBUtils.queryForListCallProc(procAndParameters));
    }

    @ApiOperation(value = "执行一个查询SQL", httpMethod = "POST", notes = "传入一个对象，只有一个属性sql。例如：{\"sql\":\"select * from barrel\"}")
    @PostMapping("/queryForListSQL")
    public JsonResult queryForListSQL (@RequestBody Map<String,String> params){
        String sql = params.get("sql");
        return JsonResult.ok(dynamicDBUtils.queryForList(sql));
    }

    @ApiOperation(value = "执行增删该SQL", httpMethod = "POST", notes = "")
    @PostMapping("/cud")
    public JsonResult cud (@RequestBody String sql){
        return JsonResult.ok(dynamicDBUtils.cud(sql));
    }
}




